<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2018/1/4
 * Time: 19:01
 */

namespace Admin\Logic;
use Admin\Transformer\OrderTransformer;
use Think\Model;

class StatLogic extends Model
{
    protected $autoCheckFields = false;

    /**
     * 业支客户统计
     * @param $params
     * @return mixed
     */
    public function yzStatCustomerList($params){
        $result = D('Resource/Customer','Service')->searchCustomerListByCompany($params);
        if(is_array($result['dataList']) && count($result['dataList'])>0){
            foreach ($result['dataList'] as $k=>$customer){
                $result['dataList'][$k]['order_sum'] = D('Resource/Order')->getOrderCount(['company_name'=>$customer['company_name']]);
                $oneOrder = D('Resource/Order')->getOneOrderInfo(['company_name'=>$customer['company_name']]);
                $result['dataList'][$k]['order_time'] = '';
                if($oneOrder){
                    $result['dataList'][$k]['order_time'] = date("Y-m-d",$oneOrder['create_time']);
                }
            }
        }
        return $result;
    }

    /**
     * 业支客户订单列表
     * @param $params
     * @return mixed
     */
    public function yzStatCustomerOrderList($params){
        $result = D('Resource/Order','Service')->searchOrderListByCompany($params);
        if(is_array($result['orderList']) && count($result['orderList'])>0){
            foreach ($result['orderList'] as $k=>$order){
                //客户
                $customer = D('Resource/Customer','Service')->getCustomerInfo(['id'=>$order['customer_id']]);
                $result['orderList'][$k]['product_name'] = $customer['product_name'];
                //查询部门信息
                $departmentInfo = D('Resource/User', 'Service')->getDepartmentInfo(['id'=>$order['department_id']]);
                $result['orderList'][$k]['department_name'] = $departmentInfo['name'];
            }
            $tr = new OrderTransformer();
            $result['orderList'] = $tr->TransformerCollection($result['orderList']);
        }
       return $result;
    }

    /**
     * 全部业务员订单信息统计
     * @param $params
     * @return mixed
     */
    public function staffStatList($params){
        $name = $params['name'];
        $group_id = $params['group_id'];
        $department_id = $params['department_id'];

        $data = [];
        $name && $data['au.name'] = ['like',"%{$name}%"];
        $group_id && $data['aug.id'] = $group_id;
        $department_id && $data['au.department_id'] = $department_id;

        $userList = D('Resource/User','Service')->getAllUserList($data);
        if(is_array($userList) && count($userList)>0){
            foreach ($userList as $key=>$user){
                //客户数
                $all_customer = D('Resource/Customer','Service')->getCustomerCount(['admin_id'=>$user['id']]);
                $userList[$key]['all_customer'] = $all_customer;
                //总订单数
                $data = [];
                $data['admin_id'] = $user['id'];
                $all_order = D('Resource/Order','Service')->getOrderCount($data);
                $userList[$key]['all_order'] = $all_order;
                //总成单数
                $data_o = [];
                $data_o['admin_id'] = $user['id'];
                $data_o['status'] = 55;
                $over_order = D('Resource/Order','Service')->getOrderCount($data_o);
                $userList[$key]['over_order'] = $over_order;
                //进行中单数
                $data_g = [];
                $data_g['admin_id'] = $user['id'];
                $data_g['status'] = ['in',[-5,1,5,10,15,20,25,30,35,40,45,50]];
                $going_order = D('Resource/Order','Service')->getOrderCount($data_g);
                $userList[$key]['going_order'] = $going_order;
                //销售统计
                $data_s = [];
                $data_s['admin_id'] = $user['id'];
                $data_s['status'] = 55;
                $statOrder = D('Resource/Order','Service')->getStatOrderInfo($data_s);
                $userList[$key]['total_amount'] = $statOrder['total_amount'];
                $userList[$key]['total_base_price'] = $statOrder['total_base_price'];
                $userList[$key]['total_profit_price'] = $statOrder['total_profit_price'];
                $userList[$key]['total_other_price'] = $statOrder['total_other_price'];
            }
        }
        return $userList;
    }
    /**
     * 业务员统计
     * @param $params
     * @return mixed
     */
    public function staffOneStatList($params){
        $user = D('Resource/User','Service')->getUserInfo($params);
        if($user){
            //客户数
            $all_customer = D('Resource/Customer','Service')->getCustomerCount(['admin_id'=>$user['id']]);
            $user['all_customer'] = $all_customer;
            //总订单数
            $data = [];
            $data['admin_id'] = $user['id'];
            $all_order = D('Resource/Order','Service')->getOrderCount($data);
            $user['all_order'] = $all_order;
            //总成单数
            $data_o = [];
            $data_o['admin_id'] = $user['id'];
            $data_o['status'] = 55;
            $over_order = D('Resource/Order','Service')->getOrderCount($data_o);
            $user['over_order'] = $over_order;
            //进行中单数
            $data_g = [];
            $data_g['admin_id'] = $user['id'];
            $data_g['status'] = ['in',[-5,1,5,10,15,20,25,30,35,40,45,50]];
            $going_order = D('Resource/Order','Service')->getOrderCount($data_g);
            $user['going_order'] = $going_order;
            //销售统计
            $data_s = [];
            $data_s['admin_id'] = $user['id'];
            $data_s['status'] = 55;
            $statOrder = D('Resource/Order','Service')->getStatOrderInfo($data_s);
            $user['total_amount'] = $statOrder['total_amount'];
            $user['total_base_price'] = $statOrder['total_base_price'];
            $user['total_profit_price'] = $statOrder['total_profit_price'];
            $user['total_other_price'] = $statOrder['total_other_price'];
            //用户组
            $userGroup = D('Resource/User','Service')->getUserRelationGroupInfo(['au.id'=>$user['id']]);
            $user['user_group'] = $userGroup['name'];
            $user['user_group_id'] = $userGroup['id'];

        }
        return $user;
    }
}